package com.originskyseed.platform.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.PlayerMapper;
import com.originskyseed.platform.user.mapper.PlayerRelateChangeChannelMapper;
import com.originskyseed.platform.user.model.Player;
import com.originskyseed.platform.user.model.PlayerRelateChangeChannel;
import com.originskyseed.platform.user.service.IPlayerRelateChangeChannelService;

/**
 * 玩家渠道变化信息表Service接口实现类（OSI_PLAYER_RELATE_CHANGE_CHANNEL）
 *
 * @author WuZhuoXi
 */
@DubboService
@Component("playerRelateChangeChannelService")
public class PlayerRelateChangeChannelServiceImpl implements IPlayerRelateChangeChannelService {

	@Resource
	private PlayerRelateChangeChannelMapper playerRelateChangeChannelMapper;

	@Resource
	private PlayerMapper playerMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.playerRelateChangeChannelMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.playerRelateChangeChannelMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(PlayerRelateChangeChannel record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateChangeChannelMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(PlayerRelateChangeChannel record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateChangeChannelMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(PlayerRelateChangeChannel record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateChangeChannelMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(PlayerRelateChangeChannel record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateChangeChannelMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public PlayerRelateChangeChannel selectByPrimaryKey(String id) throws Exception {
		return this.playerRelateChangeChannelMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PlayerRelateChangeChannel> selectList(PlayerRelateChangeChannel record) throws Exception {
		return this.playerRelateChangeChannelMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateChangeChannel> selectListByPage(Map<String, Object> params, BasePage<PlayerRelateChangeChannel> page) throws Exception {
		params.put("page", page);
		List<PlayerRelateChangeChannel> list = this.playerRelateChangeChannelMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertPlayerRelateChangeChannel(PlayerRelateChangeChannel record) throws Exception {
		/*
		 * List<Player> playerList = playerMapper.selectList(new Player() {{ this.setPlayerId(record.getPlayerId()); }});
		 */

		Player player = playerMapper.selectPlayerByPlayerId(record.getPlayerId());
		if (player == null) {
			return 0;
		}
		record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		record.setOldChannelId(player.getChannelId());
		record.setOldChannelSubId(player.getChannelSubId());
		record.setPlayerId(player.getPlayerId());
		record.setNickName(player.getNickName());
		record.setCreateTime(new Date());
		return this.playerRelateChangeChannelMapper.insertSelective(record);

		/*
		 * player.setChannelId(record.getChannelId()); player.setChannelSubId(record.getChannelSubId()); player.setUpdateTime(new Date()); return playerMapper.updateByPrimaryKeySelective(player);
		 */
	}

}
